﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Common.Public.InterFace;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Model.Public;

namespace RFIDStockManagement.Client.Admin
{
    public partial class UserManagerForm : XtraForm, ISwitchMainForm
    {
        
        private bool isRealColse;
        private RUser _curUser;
        private UserManager _manager;
        private UserEditerForm _editForm;
        private ChangePwdForm _pwdForm;


        public UserManagerForm(RUser logUser)
        {
            if (logUser == null)
            {
                MessageBox.Show("当前登录用户信息错误,请重新登录!","登录错误");
                this.SwitchCloseForm();
            }

            if (!logUser.IsLogin)
            {
                MessageBox.Show("当前登录用户信息错误,请重新登录!", "登录错误");
                this.SwitchCloseForm();
            }

            _curUser = logUser;
            _manager = new UserManager();
            InitializeComponent();
            isRealColse = true;
            gridView1.MouseUp += new MouseEventHandler(gridView1_MouseUp);
            gridView1.DoubleClick += new EventHandler(gridView1_DoubleClick);

            init();

            
        }


        /// <summary>
        /// 修改界面
        /// </summary>
        public ChangePwdForm PwdForm
        {
            get 
            {
                if (_pwdForm == null)
                {
                    _pwdForm = new ChangePwdForm(_manager);
                }
                return _pwdForm; 
            }
            set { _pwdForm = value; }
        }

        /// <summary>
        /// 用户信息编辑界面
        /// </summary>
        protected UserEditerForm EditForm
        {
            get
            {
                if (_editForm == null)
                {
                    _editForm = new UserEditerForm(_manager);
                }
                return _editForm;
            }
            set
            {
                _editForm = value;
            }
        }


        #region ISwitchMainForm 成员

        public bool IsRealCloseForm
        {
            get
            {
                return isRealColse;
            }
            set
            {
                isRealColse = value;
            }
        }

        public void RealCloseForm()
        {
            isRealColse = true;
            this.Close();
        }

        public void SwitchCloseForm()
        {
            isRealColse = false;
            this.Close();
        }

        #endregion

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _curUser.Logout();
            this.RealCloseForm();
        }

        private void enmLogout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _curUser.Logout();
            this.SwitchCloseForm();
        }



        private void init()
        {
            gridView1.ShowLoadingPanel();
            List<UserInfo> users = _manager.GetUsers();
            BindData(users);
            gridView1.HideLoadingPanel();
        }

        private void BindData(List<UserInfo> infos)
        {
            DataTable newTable = new DataTable();
            DataRow newRow;
            newTable.Columns.Add("FID", typeof(int));
            newTable.Columns.Add("FlogName", typeof(string));
            newTable.Columns.Add("FName", typeof(string));
            newTable.Columns.Add("FSex", typeof(string));
            newTable.Columns.Add("FTel", typeof(string));
            newTable.Columns.Add("FLastLoginTime", typeof(string));
            newTable.Columns.Add("FLastLogoutTime", typeof(string));
            newTable.Columns.Add("FIsAdmin", typeof(string));


            foreach (UserInfo x in infos)
            {
                newRow = newTable.NewRow();
                newRow["FID"] = x.Id;
                newRow["FlogName"] = x.LogName;
                newRow["FName"] = x.Name;
                newRow["FSex"] = x.IsMale?"男":"女";
                newRow["FTel"] = x.TelNumber;
                newRow["FLastLoginTime"] = x.LastLoginTime.ToString(Common.Public.PublicConst.ShortTimeFormater);
                newRow["FLastLogoutTime"] = x.LastLogoutTime.ToString(Common.Public.PublicConst.ShortTimeFormater);
                newRow["FIsAdmin"] = x.IsAdmin ? "是" : "否";

                newTable.Rows.Add(newRow);
            }

            gridControl1.DataSource = newTable;

        }


        private void gridView1_MouseUp(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            } 
        }

        private void enmModifyUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           int id = GetCurrentSelectID();
           if (id > 0)
           {
               EditForm.ModifyUser(id);
               if (EditForm.IsOperatOk)
               {
                   init();
               }
           }
        }


        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            enmModifyUser_ItemClick(sender, null);
        }


        private void enmAddUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditForm.AddUser();
            if (EditForm.IsOperatOk)
            {
                init();
            }
        }

        private void enmDeleteUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            int id = GetCurrentSelectID();
            if (id > 0)
            {
                if (!AlertForm.Confirm("是否确认删除当前选中用户?", "删除确认"))
                {
                    return;
                }
                if (_manager.DeleteUser(id))
                {
                    init();
                }
            }
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bntChangePwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int id = GetCurrentSelectID();
            if (id > 0)
            {
                PwdForm.ChangeUserPwd(id);
            }
        }


        private int GetCurrentSelectID()
        {
            int[] rows = gridView1.GetSelectedRows();

            if (rows.Length <= 0)
            {
                return -1;
            }

            return int.Parse(gridView1.GetRowCellValue(rows[0], "FID").ToString());
        }



    }
}